package com.qf.hospital.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.hospital.dto.ReportDTO;
import com.qf.hospital.entity.Report;
import com.qf.hospital.entity.ReportExample;
import com.qf.hospital.enums.HospitalExceptionEnums;
import com.qf.hospital.exception.HospitalException;
import com.qf.hospital.mapper.ReportMapper;
import com.qf.hospital.service.ReportService;
import com.qf.hospital.vo.ReportVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zjw
 * @description
 * @date 2021/10/19 16:24
 */
@Service
public class ReportServiceImpl implements ReportService {

    private Logger log = LoggerFactory.getLogger(ReportServiceImpl.class);

    /**
     * 已删除
     */
    private final int DELETE = 0;

    /**
     * 预约成功
     */
    private final int SUCCESS = 1;

    /**
     * 药品缴费成功
     */
    private final int DRUG_PAY_SUCCESS = 2;
    /**
     * 药品出库成功
     */
    private final int DRUG_EX = 3;
    /**
     * 转住院
     */
    private final int HOSPITALIZATION = 4;

    @Autowired
    private ReportMapper reportMapper;

    @Override
    public Integer findReportCountTotal() {
        //1. 封装查询条件
        ReportExample example = new ReportExample();
        ReportExample.Criteria criteria = example.createCriteria();
        criteria.andStateNotEqualTo(DELETE);

        //2. 查询
        long count = reportMapper.countByExample(example);

        //3. 返回
        return Integer.valueOf(count + "");
    }

    @Override
    public Integer findReportCountCurrent() {
        //1. 调用mapper查询数据
        long count = reportMapper.findReportCountByCurrent();

        //3. 返回
        return Integer.valueOf(count + "");
    }

    @Override
    public List<ReportVO> findPatientInfo(Integer cc, String name) {
        //1. 调用Mapper查询数据
        List<Report> reportList = reportMapper.findPatientInfo(cc,name);

        //2. 封装数据
        List<ReportVO> data = new ArrayList<>();
        for (Report report : reportList) {
            ReportVO vo = new ReportVO();
            BeanUtils.copyProperties(report,vo);
            data.add(vo);
        }
        //3. 返回data
        return data;
    }

    @Override
    public Integer checkPhoneAndCarid(String phone, String carid) {
        // 调用Service查询手机号/身份证是否重复
        int count = reportMapper.checkExists("phone",phone);
        if(count >= 1){
            return 1;
        }
        count = reportMapper.checkExists("carid",carid);
        return count >= 1 ? 2 : 3;
    }

    @Override
    public void addReport(ReportDTO reportDTO) {
        //1. DTO -> Entity
        Report report = new Report();
        BeanUtils.copyProperties(reportDTO,report);
        report.setDepartment(reportDTO.getDepartmentId());
        report.setDoctor(reportDTO.getDoctorId());
        report.setReportType(reportDTO.getRegisterTypeId());

        //2. 封装state和time
        report.setTime(new Date());
        report.setState(SUCCESS);

        //3. 添加
        int count = reportMapper.insertSelective(report);

        //4. 校验
        if(count != 1){
            log.error("【添加患者信息】 添加患者信息失败！  report = {}",report);
            throw new HospitalException(HospitalExceptionEnums.REPORT_ADD_ERROR);
        }
    }

    @Override
    public PageInfo<Report> findPerson(String name, Integer page, Integer limit) {
        //1. 分页
        PageHelper.startPage(page,limit);
        //2. 执行查询
        List<Report> list = reportMapper.findPrescriptionInfo(name);
        //3. 封装PageInfo
        PageInfo<Report> pageInfo = new PageInfo<>(list);
        //4. 返回
        return pageInfo;
    }
}
